<?php
namespace app\api\controller;
use think\Db;
use think\Session;

class Vote extends  Base
{
	public function _initialize()
	{
		parent::_initialize();  //调用父类构造方法 
		$check = $this->state;
	    if($check == -1)
	    {
	    	 $err['code'] = -1;
             $err['message'] = '尚未登录';
             echo json_encode($err,JSON_UNESCAPED_UNICODE);die;
	    }

	}
	/**
	*提交投票
	*/
	public function put_vote()
	{
		if(request()->isPost())
		{
			$post = file_get_contents("php://input");
			$data = json_decode($post,true);

			if(!isset($data['sign']) || empty($data['sign']))
			{
				$info['code'] = -1;
	        	$info['message'] = '奖项标识异常';
	        	return json_encode($info,JSON_UNESCAPED_UNICODE);
			}

			$sign = $data['sign'];//奖项标识
			$ids =$data['ids'];

			//判断此用户是否已经投过此类型的票
			$account = Session::get('account','think');
			$code = db('voter',[],false)->where('account',$account)->column('status');

			if(in_array($sign,$code))
			{
				$info['code'] = -1;
				$info['message'] 	= '已经投过票了，不能重复投票了～';
				return json_encode($info,JSON_UNESCAPED_UNICODE);
			}

			//判断用户选择是否为空
			if(!isset($ids) || empty($ids))
			{
				$info['code'] = -1;
	        	$info['message'] = '不允许不选';
	        	return json_encode($info,JSON_UNESCAPED_UNICODE);
			}

			//判断用户是否选择超过限制
			if($sign == 'personal' && count($ids)>10)
			{
				$info['code'] = -1;
	        	$info['message'] = '先进个人不得超过10个';
	        	return json_encode($info,JSON_UNESCAPED_UNICODE);
			}
			if($sign == 'pacesetter' && count($ids)>5)
			{
				$info['code'] = -1;
	        	$info['message'] = '师德标兵不得超过5个';
	        	return json_encode($info,JSON_UNESCAPED_UNICODE);
			}
			

			$id = [];
			$name = [];
			//遍历获取所选候选人的id和name
			if(is_array($ids))
	       	{
		         foreach($ids as $key => $val)
		         {
		           $id[] = $val['id'];
		           $name[] = $val['name'];
		         }
	       	}
	       	$new_id = implode(',',$id); 
	       	$new_name = implode(',',$name); 

			$res = Db::name('candidate')->where('id','in', $new_id)->setInc($sign); //一次更新多行，返回更新的行数
			//投票成功
			if($res >= 0) 
			{
				//插入投票人表
				$voter['account'] = Session::get('account','think');
				//$voter['password'] = Session::get('password','think');
				$voter['status'] = $sign;
				$in_voter = db('voter',[],false)->insert($voter);
				if(!$in_voter)
				{
					$info['code'] = -1;
		        	$info['message'] = 'Mysql异常';
		        	return json_encode($info,JSON_UNESCAPED_UNICODE);
				}
				//插入投票表
				$toupiao['name'] = $new_name;
				$toupiao['prize_name'] = prize_name($sign);
				$toupiao['account'] = Session::get('account','think');
				$toupiao['time'] = time();
				$in_vote = db('vote',[],false)->insert($toupiao);
				if(!$in_vote)
				{
					$info['code'] = -1;
		        	$info['message'] = 'Mysql异常';
		        	return json_encode($info,JSON_UNESCAPED_UNICODE);
				}

				$info['code'] = 0;
	        	$info['message'] = '投票成功';
	        	return json_encode($info,JSON_UNESCAPED_UNICODE);
			}
		}else
		{
			$info['code'] = -1;
        	$info['message'] = '非法方式传输';
        	return json_encode($info,JSON_UNESCAPED_UNICODE);
		}
	}


	/**
	*测试投票(供压力测试)
	*/
	public function test_vote()
	{
		if(request()->isGet())
		{
			$id = $_GET['id'];
			$name = $_GET['name'];
			$account = $_GET['account'];
			$sign = $_GET['sign'];

			$res = Db::name('candidate')->where('id',$id)->setInc($sign); //一次更新多行，返回更新的行数
			//投票成功
			if($res >= 0) 
			{
				//插入投票人表
				$voter['account'] = $account;
				//$voter['password'] = Session::get('password','think');
				$voter['status'] = $sign;
				$in_voter = db('voter',[],false)->insert($voter);
				if(!$in_voter)
				{
					$info['code'] = -1;
		        	$info['message'] = 'Mysql异常';
		        	return json_encode($info,JSON_UNESCAPED_UNICODE);
				}
				//插入投票表
				$toupiao['name'] = $name;
				$toupiao['prize_name'] = prize_name($sign);
				$toupiao['account'] = $account;
				$toupiao['time'] = time();
				$in_vote = db('vote',[],false)->insert($toupiao);
				if(!$in_vote)
				{
					$info['code'] = -1;
		        	$info['message'] = 'Mysql异常';
		        	return json_encode($info,JSON_UNESCAPED_UNICODE);
				}

				$info['code'] = 0;
	        	$info['message'] = '投票成功';
	        	return json_encode($info,JSON_UNESCAPED_UNICODE);
			}


		}
	}



}
